GetMonthlyFairCalendarQueryHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 72
dl 0
loc 87
c 0
b 0
f 0
rs 10

4 Functions

Rating   Name   Duplication   Size   Complexity  
A buildWorkedFreeDays 0 23 2
A execute 0 16 1
A buildEvents 0 22 4
A buildLeaves 0 11 2
1
import { Inject } from '@nestjs/common';
2
import { QueryHandler } from '@nestjs/cqrs';
3
import { IEventRepository } from 'src/Domain/FairCalendar/Repository/IEventRepository';
4
import { IDateUtils } from 'src/Application/IDateUtils';
5
import { ProjectView } from 'src/Application/Project/View/ProjectView';
6
import { TaskView } from 'src/Application/Task/View/TaskView';
7
import { ILeaveRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRepository';
8
import { FairCalendarView } from '../View/FairCalendarView';
9
import { GetMonthlyFairCalendarQuery } from './GetMonthlyFairCalendarQuery';
10
import { Leave } from 'src/Domain/HumanResource/Leave/Leave.entity';
11
import { Event } from 'src/Domain/FairCalendar/Event.entity';
12
13
@QueryHandler(GetMonthlyFairCalendarQuery)
14
export class GetMonthlyFairCalendarQueryHandler {
15
  constructor(
16
    @Inject('IEventRepository')
17
    private readonly eventRepository: IEventRepository,
18
    @Inject('ILeaveRepository')
19
    private readonly leaveRepository: ILeaveRepository,
20
    @Inject('IDateUtils')
21
    private readonly dateUtils: IDateUtils
22
  ) {}
23
24
  public async execute(
25
    query: GetMonthlyFairCalendarQuery
26
  ): Promise<FairCalendarView[]> {
27
    const { date, userId } = query;
28
    const formatedDate = this.dateUtils.format(date, 'y-MM-dd');
29
30
    const [events, leaves] = await Promise.all([
31
      this.eventRepository.findMonthlyEvents(formatedDate, userId),
32
      this.leaveRepository.findMonthlyLeaves(formatedDate, userId)
33
    ]);
34
35
    return [
36
      ...this.buildEvents(events),
37
      ...this.buildLeaves(leaves),
38
      ...this.buildWorkedFreeDays(date)
39
    ];
40
  }
41
42
  private buildEvents(events: Event[]): FairCalendarView[] {
43
    const result: FairCalendarView[] = [];
44
45
    for (const event of events) {
46
      const project = event.getProject();
47
      const task = event.getTask();
48
49
      result.push(
50
        new FairCalendarView(
51
          event.getType(),
52
          event.getTime(),
53
          event.getDate(),
54
          event.getSummary(),
55
          event.getId(),
56
          project ? new ProjectView(project.getId(), project.getName()) : null,
57
          task ? new TaskView(task.getId(), task.getName()) : null
58
        )
59
      );
60
    }
61
62
    return result;
63
  }
64
65
  private buildLeaves(leaves: Leave[]): FairCalendarView[] {
66
    const result: FairCalendarView[] = [];
67
68
    for (const leave of leaves) {
69
      result.push(
70
        new FairCalendarView(leave.getType(), leave.getTime(), leave.getDate())
71
      );
72
    }
73
74
    return result;
75
  }
76
77
  private buildWorkedFreeDays(date: Date): FairCalendarView[] {
78
    const result: FairCalendarView[] = [];
79
80
    const workedFreeDays = this.dateUtils.getWorkedFreeDays(date.getFullYear());
81
82
    const monthWorkedFreeDays = workedFreeDays.filter(
83
      d => d.getMonth() === date.getMonth()
84
    );
85
86
    const sevenHoursInMinutesTime = 7 * 60;
87
88
    for (const day of monthWorkedFreeDays) {
89
      result.push(
90
        new FairCalendarView(
91
          'holiday',
92
          sevenHoursInMinutesTime,
93
          this.dateUtils.format(day, 'yyyy-MM-dd')
94
        )
95
      );
96
    }
97
98
    return result;
99
  }
100
}
101